var jobindex=0,resumesindex=0;
var choose=[],choose1=[];
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'hospital/general_times/index' + location.search,
                    add_url: 'hospital/general_times/add',
                    edit_url: 'hospital/general_times/edit',
                    del_url: 'hospital/general_times/del',
                    multi_url: 'hospital/general_times/multi',
                    import_url: 'hospital/general_times/import',
                    table: 'appointment_time',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'week_name', title: __('Week_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                       // {field: 'week_days', title: __('Week_days'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#addjobs').click(()=>{
                var choose_ids =choose.join(',');
                var html='';
                html+='<div style="padding-top:10px" class="job'+jobindex+'" data-index='+jobindex+' >'
                html+='<div class="form-group" style="font-size: 20px;">';
                html+='<label class="control-label col-xs-12 col-sm-2">详情时间段：</label>';
                html+='<div class="col-xs-4 col-sm-3">';
                html+='<input  name="row[job]['+jobindex+'][startime]" data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true"  type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-1 col-sm-1">'
                html+='<label style="padding-top: 5px"> 至</label>';
                html+='</div>';
                html+='<div class="col-xs-4 col-sm-3">';
                html+='<input  name="row[job]['+jobindex+'][endtime]" data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true"  type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-2 col-sm-1">'
                html+=' <label onclick="removejob('+jobindex+')" data-index="'+jobindex+'"  style="padding-top: 5px"> 删除</label>'
                html+='</div>';
                html+='</div>';
                jobindex=jobindex+1;

                $('#jobs').prepend(html);


            });

            $('#addresumes').click(()=>{
                var html='';
                html+='<div style="padding-top:10px" class="resumes'+resumesindex+'" data-index='+resumesindex+' >'

                html+='<div class="form-group" style="font-size: 20px">'
                html+='<label class="control-label col-xs-12 col-sm-2" style="font-size: 20px">预约时间段：</label>'
                html+='<div class="col-xs-4 col-sm-3" >';
                html+='<input  data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true"   name="row[resume]['+resumesindex+'][startime]" type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-1 col-sm-1">'
                html+='<label style="padding-top: 5px"> 至</label>';
                html+='</div>';
                html+='<div class="col-xs-4 col-sm-3" >';

                html+='<input  data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true" name="row[resume]['+resumesindex+'][endtime]"   type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-2 col-sm-1">'
                html+=' <label onclick="removeresume('+resumesindex+')" data-index="'+resumesindex+'"  style="padding-top: 5px"> 删除</label>'
                html+='</div>';
                html+='</div>';

                html+=' </div>';

                $('#resumes').prepend(html);

                resumesindex++;

            });
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(){
                //通用的表单组件事件绑定和组件渲染
                Form.events.datetimepicker($("form"));
            });
            Controller.api.bindevent();
        },
        edit: function () {
            jobindex=Config.jobs.length;
            Config.jobs.forEach(function(item,index) {
                var m={'endtime':item.endtime,'startime':item.startime};
                choose.splice(index,0,m);
            });
            resumesindex=Config.resumes.length;
            Config.resumes.forEach(function(item,index) {
                var m={'endtime':item.endtime,'startime':item.startime};
                choose1.splice(index,0,m)
            });

            $('#addjobs').click(()=>{
                var choose_ids =choose.join(',');
                var html='';
                html+='<div style="padding-top:10px" class="job'+jobindex+'" data-index='+jobindex+' >'
                html+='<div class="form-group" style="font-size: 20px;">';
                html+='<label class="control-label col-xs-12 col-sm-2">详情时间段：</label>';
                html+='<div class="col-xs-4 col-sm-3">';
                html+='<input  name="row[job]['+jobindex+'][startime]" data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true"  type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-1 col-sm-1">'
                html+='<label style="padding-top: 5px"> 至</label>';
                html+='</div>';
                html+='<div class="col-xs-4 col-sm-3">';
                html+='<input  name="row[job]['+jobindex+'][endtime]" data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true"  type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-2 col-sm-1">'
                html+=' <label onclick="removejob('+jobindex+')" data-index="'+jobindex+'"  style="padding-top: 5px"> 删除</label>'
                html+='</div>';
                html+='</div>';
                jobindex=jobindex+1;

                $('#jobs').prepend(html);


            });

            $('#addresumes').click(()=>{
                var html='';
                html+='<div style="padding-top:10px" class="resumes'+resumesindex+'" data-index='+resumesindex+' >'

                html+='<div class="form-group" style="font-size: 20px">'
                html+='<label class="control-label col-xs-12 col-sm-2" style="font-size: 20px">预约时间段：</label>'
                html+='<div class="col-xs-4 col-sm-3" >';
                html+='<input  data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true"   name="row[resume]['+resumesindex+'][startime]" type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-1 col-sm-1">'
                html+='<label style="padding-top: 5px"> 至</label>';
                html+='</div>';
                html+='<div class="col-xs-4 col-sm-3" >';

                html+='<input  data-rule="required" class="form-control datetimepicker" data-date-format="HH:mm" data-use-current="true" name="row[resume]['+resumesindex+'][endtime]"   type="text" value="">';
                html+='</div>';
                html+='<div class="col-xs-2 col-sm-1">'
                html+=' <label onclick="removeresume('+resumesindex+')" data-index="'+resumesindex+'"  style="padding-top: 5px"> 删除</label>'
                html+='</div>';
                html+='</div>';

                html+=' </div>';

                $('#resumes').prepend(html);

                resumesindex++;

            });
            $(document).on("fa.event.appendfieldlist", '.btn-append', function(){
                //通用的表单组件事件绑定和组件渲染
                Form.events.datetimepicker($("form"));
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
function removeresume(index){
    $('.resumes'+index).remove();
    resumesindex--;


}
function removejob(index){
    $('.job'+index).remove();
    jobindex--;

}
function formatDate(date){
    // 1. 验证
    if(!date instanceof Date){
        return;
    }

    // 2. 转化
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getMinutes();

    // 3. 转化格式 YYYY-MM-dd hh:mm:ss

    // 过滤小于10的情况
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    hour = hour < 10 ? '0' + hour : hour;
    minute = minute < 10 ? '0' + minute : minute;
    second = second < 10 ? '0' + second : second;
    return  hour + ':' + minute ;
}